{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "%matplotlib notebook"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [],
   "source": [
    "#!jupyter nbconvert --to script net.ipynb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import scipy\n",
    "import sys\n",
    "import os\n",
    "import numpy as np\n",
    "import caffe\n",
    "\n",
    "import matplotlib\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [],
   "source": [
    "USED_MODEL = 'vgg16'\n",
    "SUBTRACT_MEAN = False\n",
    "SWITCH_R_AND_B = False\n",
    "TEST_IMAGE = 'data/images/dog.jpg'\n",
    "MEANS = np.array([103.939, 116.779, 123.68], dtype=np.int)\n",
    "\n",
    "MODELS = {\n",
    "    'vgg16': {\n",
    "        'weights':'model/VGG_ILSVRC_16_layers.caffemodel',\n",
    "        'model':'model/VGG_ILSVRC_16_layers_deploy.prototxt'\n",
    "    },\n",
    "    'vgg19': {\n",
    "        'weights':'model/VGG_ILSVRC_19_layers.caffemodel',\n",
    "        'model':'model/VGG_ILSVRC_19_layers_deploy.prototxt'\n",
    "    }\n",
    "}\n",
    "\n",
    "assert(USED_MODEL in MODELS)\n",
    "\n",
    "weights = MODELS[USED_MODEL]['weights']\n",
    "model = MODELS[USED_MODEL]['model']\n",
    "\n",
    "assert os.path.exists(weights), 'Weights file missing! {}'.format(weights)\n",
    "assert os.path.exists(model), 'Model file missing! {}'.format(model)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def get_img(path):\n",
    "    return np.moveaxis(np.array(scipy.misc.imread(path), dtype=np.int), 2, 0)\n",
    "\n",
    "def show_img(img):\n",
    "    if img.shape[0] == 3:\n",
    "        img = np.moveaxis(np.array(img, dtype=np.float32), 0, 2)\n",
    "    plt.imshow(img * 255)\n",
    "    plt.show()\n",
    "\n",
    "def top_k(elements, labels, k = 5): \n",
    "    top = elements.argsort()[-k:][::-1]\n",
    "    probs = elements[top]\n",
    "    return list(zip(probs, np.array(labels)[top]))\n",
    "\n",
    "def print_top_k(elements, labels, k = 5):\n",
    "    print(\"\\n\".join(['{:.4f} {}'.format(prob, name[10:]) for prob, name in top_k(elements, labels, k = k)]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "labels = open('data/labels.txt').read().split('\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [],
   "source": [
    "net = caffe.Classifier(model, weights)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/javascript": [
       "/* Put everything inside the global mpl namespace */\n",
       "window.mpl = {};\n",
       "\n",
       "\n",
       "mpl.get_websocket_type = function() {\n",
       "    if (typeof(WebSocket) !== 'undefined') {\n",
       "        return WebSocket;\n",
       "    } else if (typeof(MozWebSocket) !== 'undefined') {\n",
       "        return MozWebSocket;\n",
       "    } else {\n",
       "        alert('Your browser does not have WebSocket support.' +\n",
       "              'Please try Chrome, Safari or Firefox ≥ 6. ' +\n",
       "              'Firefox 4 and 5 are also supported but you ' +\n",
       "              'have to enable WebSockets in about:config.');\n",
       "    };\n",
       "}\n",
       "\n",
       "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n",
       "    this.id = figure_id;\n",
       "\n",
       "    this.ws = websocket;\n",
       "\n",
       "    this.supports_binary = (this.ws.binaryType != undefined);\n",
       "\n",
       "    if (!this.supports_binary) {\n",
       "        var warnings = document.getElementById(\"mpl-warnings\");\n",
       "        if (warnings) {\n",
       "            warnings.style.display = 'block';\n",
       "            warnings.textContent = (\n",
       "                \"This browser does not support binary websocket messages. \" +\n",
       "                    \"Performance may be slow.\");\n",
       "        }\n",
       "    }\n",
       "\n",
       "    this.imageObj = new Image();\n",
       "\n",
       "    this.context = undefined;\n",
       "    this.message = undefined;\n",
       "    this.canvas = undefined;\n",
       "    this.rubberband_canvas = undefined;\n",
       "    this.rubberband_context = undefined;\n",
       "    this.format_dropdown = undefined;\n",
       "\n",
       "    this.image_mode = 'full';\n",
       "\n",
       "    this.root = $('<div/>');\n",
       "    this._root_extra_style(this.root)\n",
       "    this.root.attr('style', 'display: inline-block');\n",
       "\n",
       "    $(parent_element).append(this.root);\n",
       "\n",
       "    this._init_header(this);\n",
       "    this._init_canvas(this);\n",
       "    this._init_toolbar(this);\n",
       "\n",
       "    var fig = this;\n",
       "\n",
       "    this.waiting = false;\n",
       "\n",
       "    this.ws.onopen =  function () {\n",
       "            fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n",
       "            fig.send_message(\"send_image_mode\", {});\n",
       "            if (mpl.ratio != 1) {\n",
       "                fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n",
       "            }\n",
       "            fig.send_message(\"refresh\", {});\n",
       "        }\n",
       "\n",
       "    this.imageObj.onload = function() {\n",
       "            if (fig.image_mode == 'full') {\n",
       "                // Full images could contain transparency (where diff images\n",
       "                // almost always do), so we need to clear the canvas so that\n",
       "                // there is no ghosting.\n",
       "                fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n",
       "            }\n",
       "            fig.context.drawImage(fig.imageObj, 0, 0);\n",
       "        };\n",
       "\n",
       "    this.imageObj.onunload = function() {\n",
       "        this.ws.close();\n",
       "    }\n",
       "\n",
       "    this.ws.onmessage = this._make_on_message_function(this);\n",
       "\n",
       "    this.ondownload = ondownload;\n",
       "}\n",
       "\n",
       "mpl.figure.prototype._init_header = function() {\n",
       "    var titlebar = $(\n",
       "        '<div class=\"ui-dialog-titlebar ui-widget-header ui-corner-all ' +\n",
       "        'ui-helper-clearfix\"/>');\n",
       "    var titletext = $(\n",
       "        '<div class=\"ui-dialog-title\" style=\"width: 100%; ' +\n",
       "        'text-align: center; padding: 3px;\"/>');\n",
       "    titlebar.append(titletext)\n",
       "    this.root.append(titlebar);\n",
       "    this.header = titletext[0];\n",
       "}\n",
       "\n",
       "\n",
       "\n",
       "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n",
       "\n",
       "}\n",
       "\n",
       "\n",
       "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n",
       "\n",
       "}\n",
       "\n",
       "mpl.figure.prototype._init_canvas = function() {\n",
       "    var fig = this;\n",
       "\n",
       "    var canvas_div = $('<div/>');\n",
       "\n",
       "    canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n",
       "\n",
       "    function canvas_keyboard_event(event) {\n",
       "        return fig.key_event(event, event['data']);\n",
       "    }\n",
       "\n",
       "    canvas_div.keydown('key_press', canvas_keyboard_event);\n",
       "    canvas_div.keyup('key_release', canvas_keyboard_event);\n",
       "    this.canvas_div = canvas_div\n",
       "    this._canvas_extra_style(canvas_div)\n",
       "    this.root.append(canvas_div);\n",
       "\n",
       "    var canvas = $('<canvas/>');\n",
       "    canvas.addClass('mpl-canvas');\n",
       "    canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n",
       "\n",
       "    this.canvas = canvas[0];\n",
       "    this.context = canvas[0].getContext(\"2d\");\n",
       "\n",
       "    var backingStore = this.context.backingStorePixelRatio ||\n",
       "\tthis.context.webkitBackingStorePixelRatio ||\n",
       "\tthis.context.mozBackingStorePixelRatio ||\n",
       "\tthis.context.msBackingStorePixelRatio ||\n",
       "\tthis.context.oBackingStorePixelRatio ||\n",
       "\tthis.context.backingStorePixelRatio || 1;\n",
       "\n",
       "    mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n",
       "\n",
       "    var rubberband = $('<canvas/>');\n",
       "    rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n",
       "\n",
       "    var pass_mouse_events = true;\n",
       "\n",
       "    canvas_div.resizable({\n",
       "        start: function(event, ui) {\n",
       "            pass_mouse_events = false;\n",
       "        },\n",
       "        resize: function(event, ui) {\n",
       "            fig.request_resize(ui.size.width, ui.size.height);\n",
       "        },\n",
       "        stop: function(event, ui) {\n",
       "            pass_mouse_events = true;\n",
       "            fig.request_resize(ui.size.width, ui.size.height);\n",
       "        },\n",
       "    });\n",
       "\n",
       "    function mouse_event_fn(event) {\n",
       "        if (pass_mouse_events)\n",
       "            return fig.mouse_event(event, event['data']);\n",
       "    }\n",
       "\n",
       "    rubberband.mousedown('button_press', mouse_event_fn);\n",
       "    rubberband.mouseup('button_release', mouse_event_fn);\n",
       "    // Throttle sequential mouse events to 1 every 20ms.\n",
       "    rubberband.mousemove('motion_notify', mouse_event_fn);\n",
       "\n",
       "    rubberband.mouseenter('figure_enter', mouse_event_fn);\n",
       "    rubberband.mouseleave('figure_leave', mouse_event_fn);\n",
       "\n",
       "    canvas_div.on(\"wheel\", function (event) {\n",
       "        event = event.originalEvent;\n",
       "        event['data'] = 'scroll'\n",
       "        if (event.deltaY < 0) {\n",
       "            event.step = 1;\n",
       "        } else {\n",
       "            event.step = -1;\n",
       "        }\n",
       "        mouse_event_fn(event);\n",
       "    });\n",
       "\n",
       "    canvas_div.append(canvas);\n",
       "    canvas_div.append(rubberband);\n",
       "\n",
       "    this.rubberband = rubberband;\n",
       "    this.rubberband_canvas = rubberband[0];\n",
       "    this.rubberband_context = rubberband[0].getContext(\"2d\");\n",
       "    this.rubberband_context.strokeStyle = \"#000000\";\n",
       "\n",
       "    this._resize_canvas = function(width, height) {\n",
       "        // Keep the size of the canvas, canvas container, and rubber band\n",
       "        // canvas in synch.\n",
       "        canvas_div.css('width', width)\n",
       "        canvas_div.css('height', height)\n",
       "\n",
       "        canvas.attr('width', width * mpl.ratio);\n",
       "        canvas.attr('height', height * mpl.ratio);\n",
       "        canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n",
       "\n",
       "        rubberband.attr('width', width);\n",
       "        rubberband.attr('height', height);\n",
       "    }\n",
       "\n",
       "    // Set the figure to an initial 600x600px, this will subsequently be updated\n",
       "    // upon first draw.\n",
       "    this._resize_canvas(600, 600);\n",
       "\n",
       "    // Disable right mouse context menu.\n",
       "    $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n",
       "        return false;\n",
       "    });\n",
       "\n",
       "    function set_focus () {\n",
       "        canvas.focus();\n",
       "        canvas_div.focus();\n",
       "    }\n",
       "\n",
       "    window.setTimeout(set_focus, 100);\n",
       "}\n",
       "\n",
       "mpl.figure.prototype._init_toolbar = function() {\n",
       "    var fig = this;\n",
       "\n",
       "    var nav_element = $('<div/>')\n",
       "    nav_element.attr('style', 'width: 100%');\n",
       "    this.root.append(nav_element);\n",
       "\n",
       "    // Define a callback function for later on.\n",
       "    function toolbar_event(event) {\n",
       "        return fig.toolbar_button_onclick(event['data']);\n",
       "    }\n",
       "    function toolbar_mouse_event(event) {\n",
       "        return fig.toolbar_button_onmouseover(event['data']);\n",
       "    }\n",
       "\n",
       "    for(var toolbar_ind in mpl.toolbar_items) {\n",
       "        var name = mpl.toolbar_items[toolbar_ind][0];\n",
       "        var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
       "        var image = mpl.toolbar_items[toolbar_ind][2];\n",
       "        var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
       "\n",
       "        if (!name) {\n",
       "            // put a spacer in here.\n",
       "            continue;\n",
       "        }\n",
       "        var button = $('<button/>');\n",
       "        button.addClass('ui-button ui-widget ui-state-default ui-corner-all ' +\n",
       "                        'ui-button-icon-only');\n",
       "        button.attr('role', 'button');\n",
       "        button.attr('aria-disabled', 'false');\n",
       "        button.click(method_name, toolbar_event);\n",
       "        button.mouseover(tooltip, toolbar_mouse_event);\n",
       "\n",
       "        var icon_img = $('<span/>');\n",
       "        icon_img.addClass('ui-button-icon-primary ui-icon');\n",
       "        icon_img.addClass(image);\n",
       "        icon_img.addClass('ui-corner-all');\n",
       "\n",
       "        var tooltip_span = $('<span/>');\n",
       "        tooltip_span.addClass('ui-button-text');\n",
       "        tooltip_span.html(tooltip);\n",
       "\n",
       "        button.append(icon_img);\n",
       "        button.append(tooltip_span);\n",
       "\n",
       "        nav_element.append(button);\n",
       "    }\n",
       "\n",
       "    var fmt_picker_span = $('<span/>');\n",
       "\n",
       "    var fmt_picker = $('<select/>');\n",
       "    fmt_picker.addClass('mpl-toolbar-option ui-widget ui-widget-content');\n",
       "    fmt_picker_span.append(fmt_picker);\n",
       "    nav_element.append(fmt_picker_span);\n",
       "    this.format_dropdown = fmt_picker[0];\n",
       "\n",
       "    for (var ind in mpl.extensions) {\n",
       "        var fmt = mpl.extensions[ind];\n",
       "        var option = $(\n",
       "            '<option/>', {selected: fmt === mpl.default_extension}).html(fmt);\n",
       "        fmt_picker.append(option)\n",
       "    }\n",
       "\n",
       "    // Add hover states to the ui-buttons\n",
       "    $( \".ui-button\" ).hover(\n",
       "        function() { $(this).addClass(\"ui-state-hover\");},\n",
       "        function() { $(this).removeClass(\"ui-state-hover\");}\n",
       "    );\n",
       "\n",
       "    var status_bar = $('<span class=\"mpl-message\"/>');\n",
       "    nav_element.append(status_bar);\n",
       "    this.message = status_bar[0];\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.request_resize = function(x_pixels, y_pixels) {\n",
       "    // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n",
       "    // which will in turn request a refresh of the image.\n",
       "    this.send_message('resize', {'width': x_pixels, 'height': y_pixels});\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.send_message = function(type, properties) {\n",
       "    properties['type'] = type;\n",
       "    properties['figure_id'] = this.id;\n",
       "    this.ws.send(JSON.stringify(properties));\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.send_draw_message = function() {\n",
       "    if (!this.waiting) {\n",
       "        this.waiting = true;\n",
       "        this.ws.send(JSON.stringify({type: \"draw\", figure_id: this.id}));\n",
       "    }\n",
       "}\n",
       "\n",
       "\n",
       "mpl.figure.prototype.handle_save = function(fig, msg) {\n",
       "    var format_dropdown = fig.format_dropdown;\n",
       "    var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n",
       "    fig.ondownload(fig, format);\n",
       "}\n",
       "\n",
       "\n",
       "mpl.figure.prototype.handle_resize = function(fig, msg) {\n",
       "    var size = msg['size'];\n",
       "    if (size[0] != fig.canvas.width || size[1] != fig.canvas.height) {\n",
       "        fig._resize_canvas(size[0], size[1]);\n",
       "        fig.send_message(\"refresh\", {});\n",
       "    };\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n",
       "    var x0 = msg['x0'] / mpl.ratio;\n",
       "    var y0 = (fig.canvas.height - msg['y0']) / mpl.ratio;\n",
       "    var x1 = msg['x1'] / mpl.ratio;\n",
       "    var y1 = (fig.canvas.height - msg['y1']) / mpl.ratio;\n",
       "    x0 = Math.floor(x0) + 0.5;\n",
       "    y0 = Math.floor(y0) + 0.5;\n",
       "    x1 = Math.floor(x1) + 0.5;\n",
       "    y1 = Math.floor(y1) + 0.5;\n",
       "    var min_x = Math.min(x0, x1);\n",
       "    var min_y = Math.min(y0, y1);\n",
       "    var width = Math.abs(x1 - x0);\n",
       "    var height = Math.abs(y1 - y0);\n",
       "\n",
       "    fig.rubberband_context.clearRect(\n",
       "        0, 0, fig.canvas.width, fig.canvas.height);\n",
       "\n",
       "    fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.handle_figure_label = function(fig, msg) {\n",
       "    // Updates the figure title.\n",
       "    fig.header.textContent = msg['label'];\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.handle_cursor = function(fig, msg) {\n",
       "    var cursor = msg['cursor'];\n",
       "    switch(cursor)\n",
       "    {\n",
       "    case 0:\n",
       "        cursor = 'pointer';\n",
       "        break;\n",
       "    case 1:\n",
       "        cursor = 'default';\n",
       "        break;\n",
       "    case 2:\n",
       "        cursor = 'crosshair';\n",
       "        break;\n",
       "    case 3:\n",
       "        cursor = 'move';\n",
       "        break;\n",
       "    }\n",
       "    fig.rubberband_canvas.style.cursor = cursor;\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.handle_message = function(fig, msg) {\n",
       "    fig.message.textContent = msg['message'];\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.handle_draw = function(fig, msg) {\n",
       "    // Request the server to send over a new figure.\n",
       "    fig.send_draw_message();\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.handle_image_mode = function(fig, msg) {\n",
       "    fig.image_mode = msg['mode'];\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.updated_canvas_event = function() {\n",
       "    // Called whenever the canvas gets updated.\n",
       "    this.send_message(\"ack\", {});\n",
       "}\n",
       "\n",
       "// A function to construct a web socket function for onmessage handling.\n",
       "// Called in the figure constructor.\n",
       "mpl.figure.prototype._make_on_message_function = function(fig) {\n",
       "    return function socket_on_message(evt) {\n",
       "        if (evt.data instanceof Blob) {\n",
       "            /* FIXME: We get \"Resource interpreted as Image but\n",
       "             * transferred with MIME type text/plain:\" errors on\n",
       "             * Chrome.  But how to set the MIME type?  It doesn't seem\n",
       "             * to be part of the websocket stream */\n",
       "            evt.data.type = \"image/png\";\n",
       "\n",
       "            /* Free the memory for the previous frames */\n",
       "            if (fig.imageObj.src) {\n",
       "                (window.URL || window.webkitURL).revokeObjectURL(\n",
       "                    fig.imageObj.src);\n",
       "            }\n",
       "\n",
       "            fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n",
       "                evt.data);\n",
       "            fig.updated_canvas_event();\n",
       "            fig.waiting = false;\n",
       "            return;\n",
       "        }\n",
       "        else if (typeof evt.data === 'string' && evt.data.slice(0, 21) == \"data:image/png;base64\") {\n",
       "            fig.imageObj.src = evt.data;\n",
       "            fig.updated_canvas_event();\n",
       "            fig.waiting = false;\n",
       "            return;\n",
       "        }\n",
       "\n",
       "        var msg = JSON.parse(evt.data);\n",
       "        var msg_type = msg['type'];\n",
       "\n",
       "        // Call the  \"handle_{type}\" callback, which takes\n",
       "        // the figure and JSON message as its only arguments.\n",
       "        try {\n",
       "            var callback = fig[\"handle_\" + msg_type];\n",
       "        } catch (e) {\n",
       "            console.log(\"No handler for the '\" + msg_type + \"' message type: \", msg);\n",
       "            return;\n",
       "        }\n",
       "\n",
       "        if (callback) {\n",
       "            try {\n",
       "                // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n",
       "                callback(fig, msg);\n",
       "            } catch (e) {\n",
       "                console.log(\"Exception inside the 'handler_\" + msg_type + \"' callback:\", e, e.stack, msg);\n",
       "            }\n",
       "        }\n",
       "    };\n",
       "}\n",
       "\n",
       "// from http://stackoverflow.com/questions/1114465/getting-mouse-location-in-canvas\n",
       "mpl.findpos = function(e) {\n",
       "    //this section is from http://www.quirksmode.org/js/events_properties.html\n",
       "    var targ;\n",
       "    if (!e)\n",
       "        e = window.event;\n",
       "    if (e.target)\n",
       "        targ = e.target;\n",
       "    else if (e.srcElement)\n",
       "        targ = e.srcElement;\n",
       "    if (targ.nodeType == 3) // defeat Safari bug\n",
       "        targ = targ.parentNode;\n",
       "\n",
       "    // jQuery normalizes the pageX and pageY\n",
       "    // pageX,Y are the mouse positions relative to the document\n",
       "    // offset() returns the position of the element relative to the document\n",
       "    var x = e.pageX - $(targ).offset().left;\n",
       "    var y = e.pageY - $(targ).offset().top;\n",
       "\n",
       "    return {\"x\": x, \"y\": y};\n",
       "};\n",
       "\n",
       "/*\n",
       " * return a copy of an object with only non-object keys\n",
       " * we need this to avoid circular references\n",
       " * http://stackoverflow.com/a/24161582/3208463\n",
       " */\n",
       "function simpleKeys (original) {\n",
       "  return Object.keys(original).reduce(function (obj, key) {\n",
       "    if (typeof original[key] !== 'object')\n",
       "        obj[key] = original[key]\n",
       "    return obj;\n",
       "  }, {});\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.mouse_event = function(event, name) {\n",
       "    var canvas_pos = mpl.findpos(event)\n",
       "\n",
       "    if (name === 'button_press')\n",
       "    {\n",
       "        this.canvas.focus();\n",
       "        this.canvas_div.focus();\n",
       "    }\n",
       "\n",
       "    var x = canvas_pos.x * mpl.ratio;\n",
       "    var y = canvas_pos.y * mpl.ratio;\n",
       "\n",
       "    this.send_message(name, {x: x, y: y, button: event.button,\n",
       "                             step: event.step,\n",
       "                             guiEvent: simpleKeys(event)});\n",
       "\n",
       "    /* This prevents the web browser from automatically changing to\n",
       "     * the text insertion cursor when the button is pressed.  We want\n",
       "     * to control all of the cursor setting manually through the\n",
       "     * 'cursor' event from matplotlib */\n",
       "    event.preventDefault();\n",
       "    return false;\n",
       "}\n",
       "\n",
       "mpl.figure.prototype._key_event_extra = function(event, name) {\n",
       "    // Handle any extra behaviour associated with a key event\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.key_event = function(event, name) {\n",
       "\n",
       "    // Prevent repeat events\n",
       "    if (name == 'key_press')\n",
       "    {\n",
       "        if (event.which === this._key)\n",
       "            return;\n",
       "        else\n",
       "            this._key = event.which;\n",
       "    }\n",
       "    if (name == 'key_release')\n",
       "        this._key = null;\n",
       "\n",
       "    var value = '';\n",
       "    if (event.ctrlKey && event.which != 17)\n",
       "        value += \"ctrl+\";\n",
       "    if (event.altKey && event.which != 18)\n",
       "        value += \"alt+\";\n",
       "    if (event.shiftKey && event.which != 16)\n",
       "        value += \"shift+\";\n",
       "\n",
       "    value += 'k';\n",
       "    value += event.which.toString();\n",
       "\n",
       "    this._key_event_extra(event, name);\n",
       "\n",
       "    this.send_message(name, {key: value,\n",
       "                             guiEvent: simpleKeys(event)});\n",
       "    return false;\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.toolbar_button_onclick = function(name) {\n",
       "    if (name == 'download') {\n",
       "        this.handle_save(this, null);\n",
       "    } else {\n",
       "        this.send_message(\"toolbar_button\", {name: name});\n",
       "    }\n",
       "};\n",
       "\n",
       "mpl.figure.prototype.toolbar_button_onmouseover = function(tooltip) {\n",
       "    this.message.textContent = tooltip;\n",
       "};\n",
       "mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to  previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n",
       "\n",
       "mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n",
       "\n",
       "mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n",
       "    // Create a \"websocket\"-like object which calls the given IPython comm\n",
       "    // object with the appropriate methods. Currently this is a non binary\n",
       "    // socket, so there is still some room for performance tuning.\n",
       "    var ws = {};\n",
       "\n",
       "    ws.close = function() {\n",
       "        comm.close()\n",
       "    };\n",
       "    ws.send = function(m) {\n",
       "        //console.log('sending', m);\n",
       "        comm.send(m);\n",
       "    };\n",
       "    // Register the callback with on_msg.\n",
       "    comm.on_msg(function(msg) {\n",
       "        //console.log('receiving', msg['content']['data'], msg);\n",
       "        // Pass the mpl event to the overriden (by mpl) onmessage function.\n",
       "        ws.onmessage(msg['content']['data'])\n",
       "    });\n",
       "    return ws;\n",
       "}\n",
       "\n",
       "mpl.mpl_figure_comm = function(comm, msg) {\n",
       "    // This is the function which gets called when the mpl process\n",
       "    // starts-up an IPython Comm through the \"matplotlib\" channel.\n",
       "\n",
       "    var id = msg.content.data.id;\n",
       "    // Get hold of the div created by the display call when the Comm\n",
       "    // socket was opened in Python.\n",
       "    var element = $(\"#\" + id);\n",
       "    var ws_proxy = comm_websocket_adapter(comm)\n",
       "\n",
       "    function ondownload(figure, format) {\n",
       "        window.open(figure.imageObj.src);\n",
       "    }\n",
       "\n",
       "    var fig = new mpl.figure(id, ws_proxy,\n",
       "                           ondownload,\n",
       "                           element.get(0));\n",
       "\n",
       "    // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n",
       "    // web socket which is closed, not our websocket->open comm proxy.\n",
       "    ws_proxy.onopen();\n",
       "\n",
       "    fig.parent_element = element.get(0);\n",
       "    fig.cell_info = mpl.find_output_cell(\"<div id='\" + id + \"'></div>\");\n",
       "    if (!fig.cell_info) {\n",
       "        console.error(\"Failed to find cell for figure\", id, fig);\n",
       "        return;\n",
       "    }\n",
       "\n",
       "    var output_index = fig.cell_info[2]\n",
       "    var cell = fig.cell_info[0];\n",
       "\n",
       "};\n",
       "\n",
       "mpl.figure.prototype.handle_close = function(fig, msg) {\n",
       "    var width = fig.canvas.width/mpl.ratio\n",
       "    fig.root.unbind('remove')\n",
       "\n",
       "    // Update the output cell to use the data from the current canvas.\n",
       "    fig.push_to_output();\n",
       "    var dataURL = fig.canvas.toDataURL();\n",
       "    // Re-enable the keyboard manager in IPython - without this line, in FF,\n",
       "    // the notebook keyboard shortcuts fail.\n",
       "    IPython.keyboard_manager.enable()\n",
       "    $(fig.parent_element).html('<img src=\"' + dataURL + '\" width=\"' + width + '\">');\n",
       "    fig.close_ws(fig, msg);\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.close_ws = function(fig, msg){\n",
       "    fig.send_message('closing', msg);\n",
       "    // fig.ws.close()\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.push_to_output = function(remove_interactive) {\n",
       "    // Turn the data on the canvas into data in the output cell.\n",
       "    var width = this.canvas.width/mpl.ratio\n",
       "    var dataURL = this.canvas.toDataURL();\n",
       "    this.cell_info[1]['text/html'] = '<img src=\"' + dataURL + '\" width=\"' + width + '\">';\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.updated_canvas_event = function() {\n",
       "    // Tell IPython that the notebook contents must change.\n",
       "    IPython.notebook.set_dirty(true);\n",
       "    this.send_message(\"ack\", {});\n",
       "    var fig = this;\n",
       "    // Wait a second, then push the new image to the DOM so\n",
       "    // that it is saved nicely (might be nice to debounce this).\n",
       "    setTimeout(function () { fig.push_to_output() }, 1000);\n",
       "}\n",
       "\n",
       "mpl.figure.prototype._init_toolbar = function() {\n",
       "    var fig = this;\n",
       "\n",
       "    var nav_element = $('<div/>')\n",
       "    nav_element.attr('style', 'width: 100%');\n",
       "    this.root.append(nav_element);\n",
       "\n",
       "    // Define a callback function for later on.\n",
       "    function toolbar_event(event) {\n",
       "        return fig.toolbar_button_onclick(event['data']);\n",
       "    }\n",
       "    function toolbar_mouse_event(event) {\n",
       "        return fig.toolbar_button_onmouseover(event['data']);\n",
       "    }\n",
       "\n",
       "    for(var toolbar_ind in mpl.toolbar_items){\n",
       "        var name = mpl.toolbar_items[toolbar_ind][0];\n",
       "        var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
       "        var image = mpl.toolbar_items[toolbar_ind][2];\n",
       "        var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
       "\n",
       "        if (!name) { continue; };\n",
       "\n",
       "        var button = $('<button class=\"btn btn-default\" href=\"#\" title=\"' + name + '\"><i class=\"fa ' + image + ' fa-lg\"></i></button>');\n",
       "        button.click(method_name, toolbar_event);\n",
       "        button.mouseover(tooltip, toolbar_mouse_event);\n",
       "        nav_element.append(button);\n",
       "    }\n",
       "\n",
       "    // Add the status bar.\n",
       "    var status_bar = $('<span class=\"mpl-message\" style=\"text-align:right; float: right;\"/>');\n",
       "    nav_element.append(status_bar);\n",
       "    this.message = status_bar[0];\n",
       "\n",
       "    // Add the close button to the window.\n",
       "    var buttongrp = $('<div class=\"btn-group inline pull-right\"></div>');\n",
       "    var button = $('<button class=\"btn btn-mini btn-primary\" href=\"#\" title=\"Stop Interaction\"><i class=\"fa fa-power-off icon-remove icon-large\"></i></button>');\n",
       "    button.click(function (evt) { fig.handle_close(fig, {}); } );\n",
       "    button.mouseover('Stop Interaction', toolbar_mouse_event);\n",
       "    buttongrp.append(button);\n",
       "    var titlebar = this.root.find($('.ui-dialog-titlebar'));\n",
       "    titlebar.prepend(buttongrp);\n",
       "}\n",
       "\n",
       "mpl.figure.prototype._root_extra_style = function(el){\n",
       "    var fig = this\n",
       "    el.on(\"remove\", function(){\n",
       "\tfig.close_ws(fig, {});\n",
       "    });\n",
       "}\n",
       "\n",
       "mpl.figure.prototype._canvas_extra_style = function(el){\n",
       "    // this is important to make the div 'focusable\n",
       "    el.attr('tabindex', 0)\n",
       "    // reach out to IPython and tell the keyboard manager to turn it's self\n",
       "    // off when our div gets focus\n",
       "\n",
       "    // location in version 3\n",
       "    if (IPython.notebook.keyboard_manager) {\n",
       "        IPython.notebook.keyboard_manager.register_events(el);\n",
       "    }\n",
       "    else {\n",
       "        // location in version 2\n",
       "        IPython.keyboard_manager.register_events(el);\n",
       "    }\n",
       "\n",
       "}\n",
       "\n",
       "mpl.figure.prototype._key_event_extra = function(event, name) {\n",
       "    var manager = IPython.notebook.keyboard_manager;\n",
       "    if (!manager)\n",
       "        manager = IPython.keyboard_manager;\n",
       "\n",
       "    // Check for shift+enter\n",
       "    if (event.shiftKey && event.which == 13) {\n",
       "        this.canvas_div.blur();\n",
       "        // select the cell after this one\n",
       "        var index = IPython.notebook.find_cell_index(this.cell_info[0]);\n",
       "        IPython.notebook.select(index + 1);\n",
       "    }\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.handle_save = function(fig, msg) {\n",
       "    fig.ondownload(fig, null);\n",
       "}\n",
       "\n",
       "\n",
       "mpl.find_output_cell = function(html_output) {\n",
       "    // Return the cell and output element which can be found *uniquely* in the notebook.\n",
       "    // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n",
       "    // IPython event is triggered only after the cells have been serialised, which for\n",
       "    // our purposes (turning an active figure into a static one), is too late.\n",
       "    var cells = IPython.notebook.get_cells();\n",
       "    var ncells = cells.length;\n",
       "    for (var i=0; i<ncells; i++) {\n",
       "        var cell = cells[i];\n",
       "        if (cell.cell_type === 'code'){\n",
       "            for (var j=0; j<cell.output_area.outputs.length; j++) {\n",
       "                var data = cell.output_area.outputs[j];\n",
       "                if (data.data) {\n",
       "                    // IPython >= 3 moved mimebundle to data attribute of output\n",
       "                    data = data.data;\n",
       "                }\n",
       "                if (data['text/html'] == html_output) {\n",
       "                    return [cell, data, j];\n",
       "                }\n",
       "            }\n",
       "        }\n",
       "    }\n",
       "}\n",
       "\n",
       "// Register the function which deals with the matplotlib target/channel.\n",
       "// The kernel may be null if the page has been refreshed.\n",
       "if (IPython.notebook.kernel != null) {\n",
       "    IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n",
       "}\n"
      ],
      "text/plain": [
       "<IPython.core.display.Javascript object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<img src=\"\" width=\"640\">"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "img = get_img(TEST_IMAGE)\n",
    "\n",
    "# DEPRECATED!\n",
    "if False:\n",
    "    for y in img:\n",
    "        for x in y:\n",
    "            if SWITCH_R_AND_B:\n",
    "                r = x[0]\n",
    "                g = x[1]\n",
    "                b = x[2]\n",
    "                x[0] = b\n",
    "                x[2] = r\n",
    "            if SUBTRACT_MEAN:\n",
    "                x -= MEANS\n",
    "\n",
    "show_img(img)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'prob': array([[  2.19705942e-09,   1.90108818e-09,   8.13242380e-08,\n",
       "           4.02037834e-08,   2.03049510e-07,   1.14694041e-08,\n",
       "           1.62148233e-08,   2.51396983e-08,   2.21549934e-09,\n",
       "           6.65738131e-09,   5.12636378e-09,   6.54464927e-09,\n",
       "           2.14505569e-09,   1.85094002e-08,   1.25503841e-09,\n",
       "           2.40630138e-09,   4.40233405e-09,   5.43763967e-09,\n",
       "           2.54780854e-08,   4.37031611e-09,   1.17130028e-09,\n",
       "           6.43054108e-08,   4.62292817e-08,   1.64626641e-08,\n",
       "           7.68757324e-09,   6.13552409e-09,   1.87534521e-09,\n",
       "           4.83481521e-09,   2.74689942e-08,   3.67150266e-09,\n",
       "           4.47526105e-09,   9.82086519e-08,   6.19995522e-09,\n",
       "           3.50383988e-09,   9.69852820e-08,   1.82736970e-09,\n",
       "           6.24467411e-09,   7.15660509e-09,   1.56327076e-07,\n",
       "           1.37621035e-08,   5.88887588e-08,   1.37989016e-08,\n",
       "           3.41109918e-09,   8.26526225e-09,   4.00657418e-09,\n",
       "           3.16895332e-09,   2.38731257e-08,   2.58049297e-08,\n",
       "           2.22471073e-08,   1.26312183e-09,   9.07189823e-10,\n",
       "           5.71182454e-07,   3.85364363e-09,   1.22399415e-08,\n",
       "           1.46825228e-08,   4.04629397e-09,   4.84826046e-09,\n",
       "           2.50147036e-09,   4.57174298e-09,   2.22769998e-08,\n",
       "           3.34575603e-08,   1.34119375e-07,   2.55724864e-08,\n",
       "           9.18017040e-09,   1.75645045e-08,   2.16501661e-08,\n",
       "           4.53197169e-09,   7.01996754e-08,   1.93342897e-09,\n",
       "           7.21622651e-10,   3.19222448e-09,   3.08451327e-08,\n",
       "           3.43442004e-08,   1.40003840e-08,   1.83831972e-08,\n",
       "           4.30408731e-09,   2.49386711e-09,   1.74020389e-08,\n",
       "           1.87197262e-08,   9.57643120e-09,   3.22029585e-08,\n",
       "           5.97541749e-09,   3.56291618e-09,   2.99947489e-09,\n",
       "           6.04026340e-09,   1.87202609e-08,   3.13410742e-09,\n",
       "           2.43478212e-07,   2.95243900e-08,   1.72914660e-08,\n",
       "           1.33985778e-09,   3.03363251e-10,   1.79246606e-09,\n",
       "           8.04449618e-10,   1.99690859e-08,   2.30411534e-09,\n",
       "           1.08980425e-08,   3.89975296e-09,   1.83091431e-09,\n",
       "           1.01604183e-08,   1.52298902e-08,   1.48514891e-06,\n",
       "           7.13788417e-10,   2.50313126e-09,   1.04861639e-08,\n",
       "           1.11404059e-08,   3.37113359e-09,   1.15784689e-08,\n",
       "           3.28383387e-09,   2.59663158e-09,   3.72063802e-09,\n",
       "           5.80186956e-08,   1.65481033e-08,   7.95273447e-09,\n",
       "           2.29416184e-08,   2.08263895e-09,   1.14907472e-09,\n",
       "           1.28744515e-09,   6.39145714e-09,   6.59978028e-09,\n",
       "           3.81901302e-08,   5.75555203e-09,   1.16681447e-08,\n",
       "           2.28363177e-08,   1.16697194e-07,   1.17172472e-09,\n",
       "           1.16324133e-08,   1.23011894e-08,   2.41885125e-08,\n",
       "           2.78831607e-08,   1.40187417e-08,   2.13710543e-08,\n",
       "           1.02326876e-08,   1.03114439e-09,   2.73486496e-08,\n",
       "           1.13351706e-09,   7.13313808e-09,   3.99822468e-08,\n",
       "           4.68653694e-09,   2.00769374e-08,   3.13898929e-09,\n",
       "           2.95413294e-09,   5.73367909e-10,   2.39589273e-08,\n",
       "           4.68008103e-08,   3.11887447e-07,   5.45966365e-08,\n",
       "           3.37310340e-08,   8.04356759e-08,   4.98955615e-08,\n",
       "           1.64702257e-07,   6.79314553e-06,   2.57032355e-07,\n",
       "           3.83948731e-07,   9.79495063e-09,   1.04994729e-06,\n",
       "           1.32454346e-07,   8.43489403e-08,   9.33046686e-05,\n",
       "           2.50213174e-03,   8.11358063e-07,   1.29116670e-04,\n",
       "           1.17155454e-04,   3.93116847e-04,   7.08629668e-04,\n",
       "           1.37415286e-02,   2.18120273e-02,   3.60604981e-03,\n",
       "           9.07479844e-04,   1.18627815e-06,   1.94664863e-05,\n",
       "           3.67100234e-04,   3.66482511e-03,   1.92996857e-04,\n",
       "           8.94104858e-07,   1.89236246e-07,   1.31054630e-03,\n",
       "           6.63167111e-06,   1.30181059e-01,   5.00862196e-04,\n",
       "           7.30320904e-03,   3.56509008e-06,   1.25920280e-06,\n",
       "           6.20168180e-07,   1.88455624e-06,   5.22106973e-07,\n",
       "           4.12852650e-08,   2.38399892e-07,   1.53453448e-06,\n",
       "           1.73213152e-07,   4.84026828e-07,   6.85158170e-07,\n",
       "           2.67338791e-08,   3.99901978e-07,   1.82609639e-07,\n",
       "           1.46607612e-03,   2.64544428e-06,   2.45260126e-05,\n",
       "           4.61323498e-06,   3.97072135e-08,   5.77783385e-06,\n",
       "           2.17421487e-07,   8.46272087e-07,   6.13531370e-07,\n",
       "           6.14053533e-07,   1.75032528e-06,   2.41610792e-06,\n",
       "           8.13964391e-07,   5.42082067e-04,   1.24060261e-05,\n",
       "           5.63412264e-04,   1.70464569e-04,   1.67245853e-05,\n",
       "           9.64474111e-07,   4.47853427e-06,   4.60212186e-06,\n",
       "           4.05032864e-07,   1.08402091e-05,   3.76435622e-07,\n",
       "           1.20613947e-07,   4.30643058e-06,   6.91429776e-08,\n",
       "           2.69760790e-06,   8.65058212e-08,   1.77738116e-07,\n",
       "           7.39631787e-07,   5.93689720e-08,   2.73446240e-05,\n",
       "           8.34921181e-08,   3.44072782e-06,   1.34404658e-07,\n",
       "           3.55859356e-07,   2.88040678e-06,   6.01723400e-08,\n",
       "           1.06573440e-04,   2.59291619e-06,   1.58870232e-03,\n",
       "           1.60430354e-05,   1.44593127e-03,   2.02750471e-06,\n",
       "           8.25063107e-05,   2.56747328e-04,   6.31603226e-03,\n",
       "           2.05349090e-04,   4.88929072e-06,   1.92136304e-06,\n",
       "           7.85767138e-01,   4.19802564e-05,   2.34514118e-05,\n",
       "           1.63178458e-06,   9.33311367e-06,   7.96934776e-03,\n",
       "           3.29595871e-08,   1.84161880e-04,   1.15844950e-05,\n",
       "           1.73092928e-07,   1.31051809e-06,   1.93805899e-05,\n",
       "           5.99167151e-08,   4.18994173e-09,   1.46417776e-08,\n",
       "           2.69587712e-08,   1.12286500e-06,   6.74567913e-09,\n",
       "           4.39653547e-07,   1.42347602e-07,   8.77108590e-08,\n",
       "           2.50041512e-06,   9.08237926e-05,   1.71795875e-07,\n",
       "           5.30649444e-08,   2.50821284e-08,   4.91836936e-08,\n",
       "           1.10571546e-05,   1.50847601e-08,   4.07324876e-07,\n",
       "           9.44500655e-09,   8.93099650e-10,   1.32990774e-09,\n",
       "           4.77642814e-09,   4.35560343e-09,   1.79779818e-08,\n",
       "           2.11190940e-08,   5.43421530e-10,   3.68380775e-08,\n",
       "           2.99486459e-07,   2.02552162e-08,   6.71544687e-09,\n",
       "           3.37827366e-08,   5.65398084e-09,   2.21970904e-08,\n",
       "           7.55214060e-08,   2.98321012e-08,   2.75556324e-08,\n",
       "           1.30380506e-08,   9.97174965e-09,   1.00832143e-08,\n",
       "           1.31651703e-08,   1.65791825e-09,   2.99221981e-08,\n",
       "           2.39161935e-08,   4.64789395e-07,   1.85362595e-07,\n",
       "           6.74010110e-08,   8.19226926e-08,   2.36657556e-08,\n",
       "           1.29621640e-06,   1.54808163e-07,   3.40527451e-09,\n",
       "           1.99731542e-09,   3.01992458e-08,   6.74062505e-07,\n",
       "           1.44167373e-07,   2.44089797e-08,   1.49392481e-08,\n",
       "           3.89361610e-08,   1.84775821e-08,   3.65447832e-08,\n",
       "           5.66674219e-09,   5.28367750e-09,   1.01378217e-09,\n",
       "           4.16144497e-09,   4.08361567e-10,   5.52420776e-09,\n",
       "           6.98355906e-09,   1.79849358e-09,   6.37874287e-09,\n",
       "           3.16445958e-09,   1.14861887e-09,   4.90032959e-10,\n",
       "           4.45341142e-09,   5.19513810e-09,   6.92204905e-10,\n",
       "           7.93815236e-10,   4.34216385e-10,   8.08296152e-10,\n",
       "           2.34383846e-10,   6.71101286e-10,   9.15039955e-09,\n",
       "           5.06101401e-07,   1.10552875e-07,   5.94675669e-08,\n",
       "           2.19870278e-09,   8.64966654e-10,   1.39757859e-08,\n",
       "           2.40715644e-06,   1.06389578e-07,   4.77803219e-09,\n",
       "           2.79634751e-08,   5.05656317e-08,   3.15416280e-08,\n",
       "           2.21534435e-08,   1.41435692e-07,   1.73403549e-07,\n",
       "           1.02024421e-06,   3.15419300e-08,   1.17262635e-08,\n",
       "           5.74144110e-09,   1.24662449e-08,   1.97952144e-08,\n",
       "           1.16681669e-08,   5.22364463e-09,   1.04206270e-08,\n",
       "           6.20616669e-09,   7.18280269e-10,   9.23749577e-10,\n",
       "           4.51972193e-09,   6.75599265e-09,   1.39559597e-09,\n",
       "           5.84488413e-09,   1.51364379e-08,   1.35143896e-08,\n",
       "           7.37828909e-09,   2.56808397e-09,   1.09217639e-08,\n",
       "           5.83683253e-08,   9.90440086e-09,   2.75300849e-09,\n",
       "           2.01051407e-08,   1.00841702e-09,   4.55464066e-09,\n",
       "           3.12801030e-09,   7.59927143e-09,   6.81486645e-09,\n",
       "           2.53994692e-09,   9.78062303e-07,   9.92558782e-08,\n",
       "           1.35645595e-09,   1.65728675e-08,   1.41726943e-07,\n",
       "           2.24261250e-08,   1.46168532e-07,   3.62655572e-09,\n",
       "           4.99121011e-09,   1.90234147e-07,   1.78351755e-07,\n",
       "           5.27057198e-09,   3.61850616e-09,   7.41392725e-08,\n",
       "           3.22405708e-06,   1.05830429e-06,   1.11018755e-06,\n",
       "           1.79665236e-07,   2.32216131e-08,   3.04133918e-09,\n",
       "           2.80914527e-08,   1.19703401e-07,   6.00350063e-08,\n",
       "           1.92001277e-08,   6.76088894e-06,   3.61748000e-08,\n",
       "           1.98935891e-06,   7.36952074e-07,   2.99077470e-07,\n",
       "           5.66957215e-06,   3.47722455e-08,   1.11698282e-05,\n",
       "           7.88346384e-08,   9.95971750e-09,   4.53250095e-08,\n",
       "           5.64809113e-07,   3.09594043e-07,   2.97342685e-05,\n",
       "           1.95592975e-06,   2.77430701e-07,   8.22308209e-07,\n",
       "           4.32764324e-07,   2.13152248e-06,   5.38273525e-06,\n",
       "           2.34726670e-07,   9.64233095e-07,   6.86859835e-07,\n",
       "           1.59161829e-07,   8.90036245e-06,   5.48275530e-05,\n",
       "           4.42193766e-07,   2.56433964e-07,   2.25325550e-07,\n",
       "           9.00652619e-09,   5.85237558e-07,   1.89186267e-07,\n",
       "           1.28412037e-08,   5.52272503e-08,   2.63853138e-07,\n",
       "           7.12216035e-07,   6.09380959e-06,   1.73084587e-08,\n",
       "           1.88640115e-06,   9.92402533e-08,   1.55274584e-08,\n",
       "           1.23445909e-06,   1.61699873e-07,   1.31596039e-07,\n",
       "           1.14953956e-07,   2.51046703e-08,   5.20207166e-08,\n",
       "           2.44745979e-06,   6.16542411e-06,   1.01949965e-07,\n",
       "           6.33639638e-06,   9.20143037e-08,   1.06258067e-05,\n",
       "           1.70959723e-07,   1.22658719e-06,   1.13763940e-06,\n",
       "           1.04802257e-06,   3.04762238e-09,   1.12220704e-07,\n",
       "           6.99017306e-08,   5.58538261e-07,   1.36911979e-07,\n",
       "           8.71749478e-07,   1.28477510e-07,   1.76391870e-06,\n",
       "           4.47987759e-07,   3.49485646e-07,   4.15267550e-07,\n",
       "           1.08756836e-07,   9.33224200e-08,   6.04970637e-07,\n",
       "           6.36457642e-08,   6.00767009e-08,   2.50915178e-07,\n",
       "           5.56922579e-08,   3.38706734e-08,   5.61767202e-08,\n",
       "           4.63321186e-08,   5.79265077e-07,   1.54522240e-06,\n",
       "           2.34855420e-07,   7.43416592e-07,   5.60444732e-07,\n",
       "           5.49020847e-08,   3.01594525e-07,   1.66127535e-07,\n",
       "           1.90487228e-07,   4.56726781e-07,   1.25087666e-07,\n",
       "           1.01658991e-07,   1.08757308e-06,   1.47205959e-09,\n",
       "           2.64567370e-06,   1.40745428e-07,   2.43825502e-06,\n",
       "           1.07636680e-07,   2.69742281e-06,   3.63078634e-08,\n",
       "           1.41493032e-07,   2.61335856e-07,   4.43119781e-08,\n",
       "           3.83557683e-08,   2.09773688e-07,   7.87232636e-07,\n",
       "           2.61875982e-07,   4.87830107e-07,   4.20768720e-06,\n",
       "           1.02140166e-06,   2.99474181e-07,   1.51149595e-06,\n",
       "           1.86426462e-07,   1.05467211e-06,   2.68594569e-08,\n",
       "           4.56839007e-06,   1.03299390e-05,   3.13835926e-06,\n",
       "           4.62547467e-09,   1.31784546e-07,   3.42977913e-08,\n",
       "           7.20238040e-06,   6.94240896e-07,   3.90635478e-06,\n",
       "           4.34187041e-06,   4.75950621e-08,   1.81375164e-08,\n",
       "           2.93040066e-05,   3.91736592e-07,   6.08292865e-08,\n",
       "           6.05815057e-06,   5.58531070e-08,   5.89942692e-06,\n",
       "           1.24866204e-07,   5.50447623e-07,   5.92714393e-08,\n",
       "           7.83652213e-05,   2.82786289e-07,   6.91087962e-07,\n",
       "           1.57208859e-07,   2.30638957e-08,   3.58430690e-08,\n",
       "           1.11462363e-07,   4.24420620e-07,   1.52061475e-08,\n",
       "           8.73599561e-07,   2.49973553e-08,   1.06455339e-07,\n",
       "           5.33159721e-07,   4.23670031e-07,   8.41327164e-07,\n",
       "           8.90091343e-08,   3.38026069e-07,   9.04628359e-06,\n",
       "           1.97754886e-07,   7.34569198e-07,   7.76156384e-09,\n",
       "           4.63839349e-08,   9.94335547e-09,   1.41660053e-07,\n",
       "           6.70393547e-07,   1.75249241e-07,   6.32879278e-08,\n",
       "           8.08575787e-05,   2.77735830e-06,   8.15673573e-08,\n",
       "           1.32632385e-08,   1.29472892e-06,   5.31808659e-08,\n",
       "           5.76324481e-08,   2.36087146e-07,   2.83881059e-06,\n",
       "           1.25580300e-06,   2.35904949e-08,   1.04287842e-07,\n",
       "           4.00637781e-08,   2.92221529e-07,   5.02572703e-08,\n",
       "           1.12653062e-07,   7.37543644e-08,   4.91629726e-06,\n",
       "           3.78824012e-07,   2.25190342e-06,   3.63112562e-08,\n",
       "           1.97678517e-07,   1.24813226e-07,   2.87774753e-07,\n",
       "           2.98392173e-08,   7.09719146e-08,   2.14708507e-06,\n",
       "           1.13296323e-06,   1.40533245e-07,   6.92162132e-08,\n",
       "           8.18514309e-07,   5.97501867e-07,   3.96771975e-06,\n",
       "           3.65485130e-06,   7.33601340e-08,   1.42453388e-07,\n",
       "           7.05260027e-06,   9.36744513e-08,   8.58476142e-06,\n",
       "           1.03251324e-07,   3.01983091e-05,   3.50031470e-08,\n",
       "           1.65207155e-06,   2.97510610e-07,   6.86502290e-07,\n",
       "           2.30746127e-06,   4.54362343e-08,   8.20190451e-07,\n",
       "           6.32272133e-07,   8.83633859e-08,   1.57471447e-07,\n",
       "           2.99562339e-06,   1.26847567e-07,   6.15280129e-08,\n",
       "           1.45266146e-08,   2.69765668e-08,   2.54000298e-07,\n",
       "           1.26448754e-07,   4.49700579e-08,   4.68907864e-08,\n",
       "           5.93938907e-08,   4.39042900e-08,   8.07812683e-07,\n",
       "           9.55781047e-07,   4.34193623e-08,   1.97722088e-07,\n",
       "           7.48021421e-07,   8.75572937e-07,   5.68987934e-06,\n",
       "           4.02101932e-06,   7.21855642e-09,   5.47230229e-07,\n",
       "           2.96866375e-07,   1.22221650e-06,   2.12605937e-06,\n",
       "           1.27442320e-07,   2.71841941e-07,   2.46735876e-08,\n",
       "           1.01667723e-07,   5.39084120e-08,   7.53259428e-07,\n",
       "           4.28939359e-07,   1.50935250e-06,   1.26741350e-06,\n",
       "           2.89895326e-08,   3.80307642e-06,   2.10983302e-07,\n",
       "           3.88885866e-08,   2.11344879e-08,   1.36433682e-08,\n",
       "           2.96759083e-07,   3.86730434e-08,   7.29614555e-08,\n",
       "           9.49019281e-08,   4.50237621e-07,   3.41096268e-07,\n",
       "           3.59366439e-08,   7.25403538e-07,   8.48747277e-08,\n",
       "           1.34629019e-07,   1.40493583e-07,   1.90757007e-07,\n",
       "           3.29124873e-07,   3.14674338e-07,   1.04126151e-07,\n",
       "           6.58539818e-08,   4.15990176e-03,   1.37357418e-07,\n",
       "           4.43049248e-06,   4.43359838e-08,   5.31063904e-07,\n",
       "           1.06244585e-07,   2.83811996e-08,   1.04887192e-07,\n",
       "           6.20446556e-08,   2.62407497e-07,   2.06296264e-07,\n",
       "           1.38574478e-07,   4.00888695e-07,   1.29091916e-06,\n",
       "           5.75604645e-07,   3.35360055e-05,   4.99947319e-08,\n",
       "           5.54434791e-07,   8.34586800e-08,   1.73715051e-07,\n",
       "           2.40979205e-07,   3.79043604e-06,   4.10760038e-07,\n",
       "           1.94553479e-07,   1.79365213e-06,   4.47887800e-07,\n",
       "           1.25558927e-05,   1.15216983e-06,   5.79754158e-07,\n",
       "           2.61797197e-08,   7.10571157e-08,   1.13204635e-06,\n",
       "           2.43842715e-06,   1.49105901e-07,   1.67743508e-06,\n",
       "           1.16987700e-07,   1.84940490e-08,   5.72327252e-08,\n",
       "           9.68129754e-09,   4.65365076e-07,   3.47368837e-06,\n",
       "           1.32764441e-07,   1.14489623e-07,   3.08351190e-07,\n",
       "           6.14272864e-08,   1.59521437e-06,   2.17988486e-07,\n",
       "           7.31106109e-09,   2.89697990e-08,   3.73156553e-07,\n",
       "           7.35372012e-08,   1.80399820e-08,   1.02362755e-07,\n",
       "           2.27342227e-07,   3.16947808e-06,   3.72776140e-06,\n",
       "           1.09033351e-06,   7.34753121e-06,   1.11419149e-08,\n",
       "           3.40683073e-07,   2.05574611e-06,   5.73159653e-08,\n",
       "           3.83102901e-08,   4.53498252e-08,   1.84956730e-06,\n",
       "           3.32308787e-07,   1.63018768e-07,   5.64356810e-07,\n",
       "           8.22711002e-08,   8.81230847e-07,   1.56698968e-06,\n",
       "           7.72705607e-06,   1.75869729e-07,   2.14437765e-08,\n",
       "           1.93658548e-06,   3.83538676e-08,   3.37154711e-06,\n",
       "           4.32854540e-06,   4.03188039e-07,   2.30542643e-08,\n",
       "           5.32594584e-07,   2.61926054e-08,   7.78263654e-07,\n",
       "           9.19297975e-07,   2.19702088e-06,   4.06094557e-07,\n",
       "           5.32039657e-09,   3.77972725e-07,   3.85593012e-07,\n",
       "           1.96194719e-06,   3.44949349e-08,   5.00266921e-08,\n",
       "           1.20229288e-06,   7.89563444e-07,   9.49322100e-07,\n",
       "           4.28171376e-07,   2.97288363e-08,   6.48767127e-07,\n",
       "           3.03814204e-07,   5.54808821e-07,   1.60294391e-07,\n",
       "           1.34141771e-07,   5.56657369e-07,   2.79917600e-08,\n",
       "           1.54254707e-08,   1.30323755e-07,   1.36444373e-06,\n",
       "           5.16509466e-08,   3.10333348e-07,   4.34783542e-05,\n",
       "           7.64001925e-06,   9.85814268e-07,   1.28148343e-07,\n",
       "           1.09527434e-06,   1.91511091e-07,   1.85371528e-06,\n",
       "           1.14411268e-05,   6.25381335e-06,   2.53128519e-07,\n",
       "           8.55908809e-07,   8.37329765e-07,   1.94385188e-06,\n",
       "           5.53323716e-08,   1.03091588e-06,   2.01326937e-08,\n",
       "           2.09923385e-07,   1.46490891e-07,   7.06105965e-08,\n",
       "           2.07246416e-08,   9.02919710e-05,   9.67814117e-07,\n",
       "           5.67814844e-08,   1.64992082e-06,   5.12805531e-09,\n",
       "           2.50019212e-07,   3.35000441e-05,   5.13319556e-08,\n",
       "           8.31393777e-07,   1.43696761e-08,   1.08304015e-07,\n",
       "           5.72772496e-09,   9.13096443e-08,   4.30368345e-06,\n",
       "           1.07850404e-07,   3.95031030e-08,   1.70200742e-08,\n",
       "           1.21419453e-06,   5.71936312e-07,   2.44165108e-07,\n",
       "           9.19057328e-08,   2.65172730e-07,   2.69480779e-06,\n",
       "           7.00820829e-06,   3.35273178e-08,   5.19569824e-07,\n",
       "           6.15680437e-06,   3.43272397e-08,   7.32416705e-09,\n",
       "           4.05793980e-06,   5.43522596e-08,   1.04847447e-06,\n",
       "           1.53240808e-06,   4.62618317e-08,   7.75475684e-09,\n",
       "           3.79434891e-06,   1.66155587e-05,   7.86820965e-06,\n",
       "           4.67303209e-07,   6.99697921e-07,   1.00764760e-06,\n",
       "           7.27895184e-08,   7.24038856e-08,   1.14294352e-07,\n",
       "           4.13501269e-08,   1.18113327e-07,   3.22645337e-06,\n",
       "           9.94721777e-05,   7.52695684e-09,   1.13853707e-06,\n",
       "           9.40966061e-08,   9.52896286e-08,   1.60578611e-06,\n",
       "           3.76255223e-08,   7.51449875e-07,   4.97999153e-08,\n",
       "           1.00617889e-07,   2.16042849e-07,   6.54588845e-08,\n",
       "           1.20054025e-07,   2.48898687e-07,   4.26916358e-07,\n",
       "           9.08491131e-08,   2.18276838e-07,   2.36363235e-06,\n",
       "           3.60659982e-07,   4.12545464e-09,   9.93612275e-07,\n",
       "           2.78519057e-08,   7.65290054e-09,   1.11265990e-07,\n",
       "           1.37300128e-06,   1.16036496e-08,   2.01403296e-07,\n",
       "           1.13949613e-06,   2.99541028e-07,   2.07079964e-07,\n",
       "           2.92917684e-05,   9.22435319e-08,   6.59228547e-08,\n",
       "           7.60474177e-06,   2.21920118e-08,   2.92715214e-07,\n",
       "           1.25328805e-08,   2.83527299e-07,   1.66264471e-07,\n",
       "           1.93455492e-08,   8.24625658e-07,   2.26503953e-08,\n",
       "           1.33076355e-06,   5.69693732e-08,   2.39296895e-07,\n",
       "           1.69696468e-05,   4.03064814e-06,   2.69770197e-07,\n",
       "           3.78121996e-07,   1.27190312e-08,   8.63854211e-07,\n",
       "           1.43838861e-07,   1.30867841e-06,   3.12490641e-07,\n",
       "           2.34351788e-07,   1.23961044e-07,   1.56070321e-08,\n",
       "           9.56168833e-08,   2.48584752e-06,   7.89946384e-08,\n",
       "           1.32733373e-06,   3.60964343e-08,   2.19798157e-09,\n",
       "           1.35108010e-07,   8.33734885e-06,   1.32562968e-06,\n",
       "           1.24482995e-07,   5.28257154e-08,   8.77523618e-08,\n",
       "           1.39603007e-05,   1.91246539e-08,   3.77056999e-08,\n",
       "           5.64442360e-09,   1.04575160e-09,   5.51917312e-09,\n",
       "           2.11895324e-09,   1.35378668e-08,   9.73281544e-07,\n",
       "           2.67603406e-09,   9.42327016e-09,   8.81155948e-09,\n",
       "           1.40231482e-09,   6.62131550e-09,   7.82775178e-09,\n",
       "           1.48528272e-07,   1.44164346e-07,   6.55098447e-08,\n",
       "           3.44452474e-07,   4.49431958e-09,   2.67157816e-08,\n",
       "           2.82474293e-08,   8.04904658e-08,   5.97526082e-08,\n",
       "           6.03548713e-08,   8.94006735e-09,   8.77841178e-09,\n",
       "           6.90438711e-08,   2.13742339e-08,   5.68577541e-09,\n",
       "           2.93449034e-08,   1.08046772e-08,   2.87907724e-08,\n",
       "           1.35674327e-07,   1.43099408e-08,   3.19175122e-08,\n",
       "           1.70908283e-08,   6.75962752e-09,   1.88602503e-08,\n",
       "           1.83491395e-07,   5.86382232e-09,   4.64434002e-09,\n",
       "           7.67749153e-09,   1.38499945e-09,   7.08486692e-09,\n",
       "           3.22401235e-07,   6.22029273e-09,   1.87918545e-07,\n",
       "           3.47349105e-09,   4.71005421e-08,   1.89629887e-07,\n",
       "           1.10253964e-07,   1.39100447e-08,   9.79470816e-09,\n",
       "           4.78976631e-07,   4.47397230e-09,   1.50779513e-07,\n",
       "           1.86894454e-07,   1.11103802e-08,   3.50370621e-09,\n",
       "           4.96412383e-07,   2.52845041e-07,   4.65615045e-07,\n",
       "           5.95816694e-08,   3.60204866e-09,   2.75762280e-09,\n",
       "           1.38368065e-07,   1.93688376e-09,   1.58089875e-07,\n",
       "           7.38410399e-09,   6.04932593e-09,   3.28631478e-09,\n",
       "           8.14897216e-09,   3.59910235e-09,   3.92161548e-09,\n",
       "           1.68334999e-08,   2.03974795e-10,   8.50529887e-08,\n",
       "           2.26600332e-06]], dtype=float32)}"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.forward(data = np.array([img]))\n",
    "#_ = net.forward()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.7858 Great Dane\n",
      "0.1302 Weimaraner\n",
      "0.0218 Walker hound, Walker foxhound\n",
      "0.0137 black-and-tan coonhound\n",
      "0.0080 dalmatian, coach dog, carriage dog\n",
      "0.0073 American Staffordshire terrier, Staffordshire terrier, American pit bull terrier, pit bull terrier\n",
      "0.0063 boxer\n",
      "0.0042 muzzle\n",
      "0.0037 whippet\n",
      "0.0036 English foxhound\n"
     ]
    }
   ],
   "source": [
    "fc8 = net.blobs['fc8'].data[0]\n",
    "prob = net.blobs['prob'].data[0]\n",
    "with open('data/fc8.txt', 'w') as f:\n",
    "    f.write(','.join([str(x) for x in fc8]))\n",
    "    f.write('\\n')\n",
    "\n",
    "prob = net.blobs['prob'].data[0]\n",
    "with open('data/prob.txt', 'w') as f:\n",
    "    f.write('\\n'.join(['{:.10f} {:<4} {}'.format(x, idx, labels[idx]) for idx, x in enumerate(prob)]))\n",
    "\n",
    "print_top_k(prob, labels, k = 10)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
